package com.link.base.base.brandmanagement.service;

import com.link.base.base.brandmanagement.dao.mybatis.mapper.BrandManagementMapper;
import com.link.base.base.brandmanagement.model.BrandManagement;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liushoulong
 * @date 2018-11-05
 * @desc 品牌管理Service
 */
@Service
public class BrandManagementServiceImpl extends BasicServiceImpl<BrandManagement> implements BrandManagementService {

    @Resource
    private BrandManagementMapper brandManagementMapper;

    @Override
    public BasicMapper<BrandManagement> getBasicMapper() {
        return brandManagementMapper;
    }


    @Override
    public void beforUpsert(BrandManagement entity) throws Exception {
        BrandManagement brandManagement = new BrandManagement();
        brandManagement.setBrandName(entity.getBrandName());
        brandManagement.setPageFlag(false);
        List<BrandManagement> brandManagementList = queryByExamplePage(brandManagement);
        boolean flag = (brandManagementList.size() == 1 && !brandManagementList.get(0).getId().equals(entity.getId()))
                || brandManagementList.size() > 1;
        if (flag) {
            throw new ServiceException("BRAND-001",entity.getBrandName());
        }
    }
}
